package com.yuqing.sentimentapp.home.presenter.impl;

import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.home.bean.RecommendDataBean;
import com.yuqing.sentimentapp.home.fragment.RecommendFragment;
import com.yuqing.sentimentapp.home.presenter.RecommendPresenter;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.yqlist.bean.YqReplyResultBean;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ShenHe on 2019/3/14.
 */

public class RecommendPresenterImpl implements RecommendPresenter {
    private RecommendFragment recommendFragment;

    public RecommendPresenterImpl(RecommendFragment recommendFragment) {
        this.recommendFragment = recommendFragment;
    }

    @Override
    public void loadRecommendDataRequest(int page, int pageSize, boolean isRefresh) {
        ModuleManager.getInstance().getRecommendDataResult(recommendFragment.getContext(), page, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RecommendDataBean>() {
                    @Override
                    protected void hideDialog() {
                        recommendFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        recommendFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(RecommendDataBean recommendDataBean) {
                        if (checkContext()) return;
                        recommendFragment.returnRecommendResult(recommendDataBean, isRefresh);
                    }

                    @Override
                    protected void onFailure(String message) {
                        recommendFragment.onFailure(message);
                    }
                });
    }

    @Override
    public void replyRequest(String replyId, String remark, int position) {
        ModuleManager.getInstance().replyRequest(recommendFragment.getContext(), replyId, remark)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<YqReplyResultBean>() {
                    @Override
                    protected void hideDialog() {
                        recommendFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        recommendFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(YqReplyResultBean baseBean) {
                        if (checkContext()) return;
                        recommendFragment.replySuccess(baseBean, position);
                    }

                    @Override
                    protected void onFailure(String message) {
                        recommendFragment.onFailure(message);
                    }
                });
    }

    @Override
    public void hideRequest(String articleId, int position) {
        ModuleManager.getInstance().postHideRequest(recommendFragment.getContext(), articleId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        recommendFragment.stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        recommendFragment.startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        if (checkContext()) return;
                        recommendFragment.hideSuccess(articleId, position);
                    }

                    @Override
                    protected void onFailure(String message) {
                        recommendFragment.onFailure(message);
                    }
                });
    }

    private boolean checkContext() {
        if (recommendFragment.getActivity() == null || recommendFragment.getActivity().isFinishing() || recommendFragment.getActivity().isDestroyed())
            return true;
        return false;
    }
}
